package com.example.springbootbabyu.config;

import com.example.springbootbabyu.intercepter.JwtAuthenticationInterceptor;
import com.example.springbootbabyu.utils.JwtTokenUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/19 22:53
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final JwtTokenUtil jwtTokenUtil;

    public WebConfig(JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173") // 允许的前端地址
                .allowedMethods("*") // 允许所有 HTTP 方法（GET, POST, OPTIONS 等）
                .allowedHeaders("*") // 允许所有请求头
                .allowCredentials(true); // 允许携带 Cookie
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JwtAuthenticationInterceptor(jwtTokenUtil))
                .addPathPatterns("/member/**")
                .excludePathPatterns(
                        "/user/**",
                        "/home/**",
                        "/category/**",
                        "/goods/**",
                        "/chat/**"
                );
    }
}